package com.jt.config;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import redis.clients.jedis.*;

import java.util.*;

/**
 * @author 张琪
 * @date 2020/12/8
 * @describe redis单台测试
 */
@Configuration
@PropertySource("properties/redis")
public class RedisConfig {

    @Value("${redis.nodes}")
    private String nodes;     //node,node,node

    @Value("${redis.config.maxtotal}")
    private int maxTotal;

    @Value("${redis.config.maxidle}")
    private int maxIdle;

    @Value("${redis.config.minidle}")
    private int minIdle;

    @Bean
    public JedisCluster jedisCluster() {
        Set<HostAndPort> set = new HashSet<>();
        String[] nodeArray = nodes.split(",");
        for (String node : nodeArray) {
            String[] nodeList = node.split(":");
            String host = nodeList[0];
            int port = Integer.parseInt(nodeList[1]);
            set.add(new HostAndPort(host, port));
        }

        JedisPoolConfig poolConfig = new JedisPoolConfig();
        poolConfig.setMaxTotal(maxTotal);
        poolConfig.setMaxIdle(maxIdle);
        poolConfig.setMinIdle(minIdle);
        return new JedisCluster(set, poolConfig);
    }
/*
    redis 哨兵模式设置
    @Value("${redis.node}")
    private String node;


    @Bean
    public JedisSentinelPool jedisSentinelPool(){
        Set<String> set = new HashSet<>();
        set.add(node);
        JedisPoolConfig poolConfig = new JedisPoolConfig();
        poolConfig.setMaxTotal(1000);
        poolConfig.setMaxIdle(40);
        poolConfig.setMinIdle(10);
        return new JedisSentinelPool("mymaster", set, poolConfig);
    }*/



/*
    redis 分片设置
    @Value("${redis.nodes}")
    private String nodes;

    @Bean
    public ShardedJedis shardedJedis(){
        List<JedisShardInfo> shardList = new ArrayList<>();
        String[] nodeArray = nodes.split(",");
        for(String node: nodeArray){
            String host = node.split(":")[0];
            int port = Integer.parseInt(node.split(":")[1]);
            JedisShardInfo shard = new JedisShardInfo(host, port);
            shardList.add(shard);
        }
        return new ShardedJedis(shardList);

    }*/


    /*
        redis单台测试
    @Value("${redis.host}")
    private String host;

    @Value("${redis.port}")
    private Integer port;

    @Bean
    public Jedis jedis(){
        System.out.println(host + " : " + port);
        return new Jedis(host,port);
    }*/
}
